﻿import com.yourdomain.spreadshop.gui.GUI;
import net.spreadshirt.shop.Shop;
import net.spreadshirt.controller.Controller;
import net.spreadshirt.logger.Logger;
import net.spreadshirt.logger.OutputPanelView;
import net.spreadshirt.logger.TextFieldView;
/**
* This class initializes the main spreadshop application.
* Use main() to create the spreadshop instance.
*/
class net.spreadshirt.Spreadshop 
{
	// Static variable. A reference to the shop instance (Singleton)
	private static var _instance : Spreadshop = null;
	// The default shop settings.
	private static var SHOP_ID : Number = 281434;
	private static var SHOP_LANG : String = "en";
	private static var SHOP_LOCALE : String = "IE";
	// Some static variables
	private static var _clip : MovieClip;
	private static var _view : GUI;
	private static var _shop : Shop;
	private static var _controller : Controller;
	/**
	* Spreadshop Constructor
	*/
	private function Spreadshop (clip : MovieClip)
	{
		// Create alias to the main movie clip
		_clip = clip;
		// Load and setup developer tool
		// Create log and observer.
		var log : Logger = Logger.getLog ();
		var outputLogView : OutputPanelView;
		outputLogView = new OutputPanelView (log);
		log.addObserver (outputLogView);
		var textLogView : TextFieldView;
		textLogView = new TextFieldView (log, _clip, 0, 10, 10, 480, 320);
		log.addObserver (textLogView);
		_clip.tfv = textLogView;
		// Setup model, view and controller
		init ();
	}
	/**
	* Creates an single instance to the Spreadshop
	*/
	public static function main (clip : MovieClip, iniProps:Object) : Void 
	{
		Spreadshop._instance = new Spreadshop (clip, iniProps);
	}
	/**
	* Setup model, view and controller.
	*/
	private function init () : Void 
	{
		// Creates model, view and controller.
		_shop = new Shop (SHOP_ID, SHOP_LANG, SHOP_LOCALE);
		_view = new GUI (_clip);
		_controller = new Controller ();
		// Connects instances.
		_controller.shop = _shop;
		_controller.view = _view;
		_view.controller = _controller;
		// Attachs listeners.
		_shop.addEventListener ('updateArticles', _view);
		_shop.addEventListener ('updateBasket', _view);
		_shop.addEventListener ('startLoading', _view);
		_shop.addEventListener ('stopLoading', _view);
		// Initializes controller.
		_controller.init ();
	}
	/**
	* Reinitializes the shop and the view.
	*/
	public static function reinit (sid : Number) : Void 
	{
		var lang : String = SHOP_LANG;
		var locale : String = SHOP_LOCALE;
		_shop.shop_id = sid;
		_shop.shop_lang = lang;
		_shop.shop_locale = locale;
		_shop.reset (sid, lang, locale);
		_shop.init ();
		_view.reset ();
	}
}
